package com.tvsinfo.suanfa;

import java.util.ArrayList;
import java.util.List;

public class Demo6 {

    public static void main(String[] args) {

        Demo6 demo6 = new Demo6();
        String[] strings= demo6.sortMethod(new String[]{"1.1.1", "1.1.2.10", "1.1.3", "1.1.2.2", "1.20.1", "1.10.1", "1.2.2", "1.3.0", "1.10.12","2.10"});
        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i]);
        }
    }

    public String[] sortMethod(String[] strings) {
        List<SortEntity> list = new ArrayList<>();
        for (int i = 0; i < strings.length; i++) {
            String str = strings[i];
            String[] split = str.replace(".", "#").split("#");
            list.add(new SortEntity(str, split, split.length));
        }

        //序号依次排序
        list.sort((o1, o2) -> {
            for (int i = 0; i < (o1.getSortLength() > o2.getSortLength() ? o1.getSortLength() : o2.getSortLength()); i++) {
                //说明到头了
                if (o1.getSortLength() < i + 1 || o2.getSortLength() < i + 1) {
                    return Integer.valueOf(o1.getSortKey()[i - 1]) > Integer.valueOf(o2.getSortKey()[i - 1]) ? 1 : 0;
                }
                int c = Integer.valueOf(o1.getSortKey()[i]);
                int d = Integer.valueOf(o2.getSortKey()[i]);
                if (c == d) {
                    continue;
                }
                return d - c;
            }
            return 1;
        });

        //
        String[] returnArray=new String[strings.length];
        for (int i = 0; i <list.size() ; i++) {
            returnArray[i]=list.get(i).getStr();
        }
        return returnArray;
    }


    class SortEntity {
        private String str;
        private String[] sortKey;
        private int sortLength;

        public SortEntity(String str, String[] sortKey, int sortLength) {
            this.str = str;
            this.sortKey = sortKey;
            this.sortLength = sortLength;
        }
        public String getStr() {
            return str;
        }
        public String[] getSortKey() {
            return sortKey;
        }
        public int getSortLength() {
            return sortLength;
        }

    }

}
